package kr.couponplay.android.support;

import android.annotation.SuppressLint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class DateUtils {
	public static int ONE_YEAR = 60 * 60 * 24 * 365;

	/**
	 * @param textDate
	 *            yyyyMMddHHmmss
	 */
	public static Date toDate(String textDate, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date date;
		try {
			date = format.parse(textDate);
		} catch (ParseException e) {
			return new Date();
		}
		return date;
	}

	public static String toString(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH시 mm분");
		return format.format(date);
	}

	public static int getTodayCount(Date startDate) {
		Date nowDate = new Date();
		int days = (int) ((nowDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
		return days + 1;
	}

	public static Date getYesterday() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		return cal.getTime();
	}

	public static String getRemainDay(long endTime) {
		long startTime = new Date().getTime();
		long remainTime = endTime - startTime;

		long oneDay = 1000 * 60 * 60 * 24;
		long oneHour = 1000 * 60 * 60;
		long oneMinute = 1000 * 60;
		if (remainTime < oneHour) {
			long result = (remainTime / oneMinute);
			return result + "분";
		} else if (remainTime < oneDay) {
			long result = (remainTime / oneHour);
			return result + "시간";
		} else {
			long result = (remainTime / oneDay);
			return result + "일";
		}
	}
}
